//
//  TokenAddressViewController.m
//  EPBPro
//
//  Created by wucha on 2018/8/1.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "TokenAddressViewController.h"
#import "HttpTokenAction.h"
#import "AddTokenAddressViewController.h"
#import "TokenAddressTableViewCell.h"

@interface TokenAddressViewController (){
    TokenABlock tokenABlock;
}
@property (nonatomic,strong) NSArray *listArray;

@end

@implementation TokenAddressViewController

-(id)initWithTokenBlock:(TokenABlock)block{
    if (self == [super init]) {
        tokenABlock = block;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *button = [UIButton new];
    [self.view addSubview:button];
    button.backgroundColor = DefaultBlueColor;
    [button setTitle:@"添加地址" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addAddressAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.sd_layout.leftSpaceToView(self.view, 20).rightSpaceToView(self.view, 20).bottomSpaceToView(self.view, 20).heightIs(40);
    
    self.titleName = @"地址";
    self.tableView.separatorStyle = 0;
    [self reloadData];
}

-(void)reloadData{
    [HttpTokenAction postTokenList:[NSDictionary dictionary] block:^(id result, NSError *error) {
        if (!error) {
            self.listArray = result;
            [self.tableView reloadData];
        }
    }];
}

-(void)addAddressAction:(id)sender{
    AddTokenAddressViewController *addTokenController = [[AddTokenAddressViewController alloc] initWithTokenBlock:^{
        [self reloadData];
    }];
    [self.navigationController pushViewController:addTokenController animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TokenAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TokenAddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    TokenAddressInfo *tokenInfo = self.listArray[indexPath.row];
    [cell setTokenAddress:tokenInfo];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TokenAddressInfo *tokenInfo = self.listArray[indexPath.row];
    if(tokenABlock){
        tokenABlock(tokenInfo);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
